<template>
  <div class="home-bg">
    <!-- 右上角用户信息 -->
    <div class="user-info-container">
      <el-dropdown @command="handleCommand" class="user-dropdown" trigger="hover">
        <div class="user-info">
          <div class="user-avatar">
            {{ getUserInitial() }}
          </div>
          <span class="user-name">{{ userStore.nickName || '用户' }}</span>
          <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="logout">
              <el-icon><SwitchButton /></el-icon>
              退出登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>

    <div class="home-logo-container">
      <img src="/logo.png" alt="logo" class="home-logo" />
      <div class="home-title">华拓工时管理系统</div>
      <div class="home-divider-wrap">
        <div class="home-divider">
          <span class="divider-text">为客户提供最优秀的产品和最极致的服务</span>
            </div>
            </div>
          </div>
  </div>
</template>

<script setup name="Index">
import { ElMessageBox } from 'element-plus'
import { ArrowDown, SwitchButton } from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()

// 获取用户名首字母
function getUserInitial() {
  const nickName = userStore.nickName || '用户'
  // 如果是中文，返回第一个字符
  if (/[\u4e00-\u9fa5]/.test(nickName)) {
    return nickName.charAt(0)
  }
  // 如果是英文，返回首字母大写
  return nickName.charAt(0).toUpperCase()
}

// 处理下拉菜单命令
function handleCommand(command) {
  if (command === 'logout') {
    logout()
  }
}

// 退出登录
function logout() {
  ElMessageBox.confirm('确定注销并退出系统吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logOut().then(() => {
      location.href = '/login'
    })
  }).catch(() => {})
}
</script>

<style scoped lang="scss">
.home-bg {
  min-height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e0e7ff 0%, #f0fdfa 100%);
  animation: bgFadeIn 1.2s;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 0;
  
  // 用户信息容器
  .user-info-container {
    position: absolute;
    top: 20px;
    right: 30px;
    z-index: 10;
    
    .user-dropdown {
      cursor: pointer;
      
      .user-info {
        display: flex;
        align-items: center;
        padding: 8px 16px;
        background: rgba(255, 255, 255, 0.9);
        backdrop-filter: blur(10px);
        border-radius: 25px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
        transition: all 0.3s ease;
        border: 1px solid rgba(255, 255, 255, 0.3);
        
        &:hover {
          background: rgba(255, 255, 255, 0.95);
          box-shadow: 0 6px 25px rgba(0, 0, 0, 0.15);
          transform: translateY(-1px);
        }
        
        .user-avatar {
          width: 32px;
          height: 32px;
          border-radius: 50%;
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
          color: white;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 14px;
          font-weight: 600;
          margin-right: 8px;
          box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
        }
        
        .user-name {
          font-size: 14px;
          font-weight: 500;
          color: #2c3e50;
          margin-right: 6px;
          max-width: 100px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        
        .dropdown-icon {
          font-size: 12px;
          color: #909399;
          transition: transform 0.3s ease;
        }
        
        &:hover .dropdown-icon {
          transform: rotate(180deg);
        }
      }
    }
  }
}

// 下拉菜单样式
:deep(.el-dropdown-menu) {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(15px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  padding: 8px 0;
  
  .el-dropdown-menu__item {
    padding: 10px 20px;
    font-size: 14px;
    color: #2c3e50;
    transition: all 0.3s ease;
    
    &:hover {
      background: rgba(102, 126, 234, 0.1);
      color: #667eea;
    }
    
    .el-icon {
      margin-right: 8px;
      font-size: 16px;
    }
  }
  
}

@keyframes bgFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
.home-logo-container {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,0.85);
  border-radius: 24px;
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.18);
  padding: 48px 64px 40px 64px;
  animation: logoFadeIn 1.2s cubic-bezier(.4,2,.6,1);
  }
@keyframes logoFadeIn {
  0% { transform: translateY(40px) scale(0.95); opacity: 0; }
  100% { transform: translateY(0) scale(1); opacity: 1; }
  }
.home-logo {
  width: 160px;
  height: 160px;
  object-fit: contain;
  margin-bottom: 32px;
  filter: drop-shadow(0 4px 16px rgba(80,120,255,0.12));
  transition: transform 0.4s cubic-bezier(.4,2,.6,1);
}
.home-logo:hover {
  transform: scale(1.08) rotate(-4deg);
}
.home-title {
  font-size: 2.4rem;
  font-weight: 800;
  color: #2d3a4b;
  letter-spacing: 6px;
  text-shadow: 0 2px 8px rgba(80,120,255,0.08);
  margin-top: 8px;
  background: linear-gradient(90deg, #3b82f6 10%, #06b6d4 90%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  }
.home-divider-wrap {
  width: 100%;
  margin-top: 36px;
  display: flex;
  justify-content: center;
}
.home-divider {
  width: 100%;
  max-width: 420px;
  text-align: center;
  position: relative;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
    }
.home-divider:before,
.home-divider:after {
  content: '';
      display: block;
  flex: 1 1 0;
  height: 2px;
  background: #222;
  opacity: 0.28;
  margin: 0 18px;
}
.divider-text {
  color: #222;
  font-size: 1.08rem;
  font-weight: 500;
  background: #fff;
  padding: 0 16px;
  letter-spacing: 2px;
  border-radius: 8px;
  z-index: 1;
  white-space: nowrap;
  overflow: visible;
  text-overflow: unset;
}

// 响应式设计
@media (max-width: 768px) {
  .home-bg {
    .user-info-container {
      top: 15px;
      right: 15px;
      
      .user-dropdown .user-info {
        padding: 6px 12px;
        
        .user-avatar {
          width: 28px;
          height: 28px;
          font-size: 12px;
        }
        
        .user-name {
          font-size: 13px;
          max-width: 80px;
        }
      }
    }
  }
  
  .home-logo-container {
    padding: 32px 24px 28px 24px;
    margin: 0 20px;
  }
  
  .home-logo {
    width: 120px;
    height: 120px;
    margin-bottom: 24px;
  }
  
  .home-title {
    font-size: 1.8rem;
    letter-spacing: 3px;
  }
  
  .divider-text {
    font-size: 0.9rem;
    padding: 0 12px;
    letter-spacing: 1px;
  }
}

@media (max-width: 480px) {
  .home-bg {
    .user-info-container {
      top: 10px;
      right: 10px;
      
      .user-dropdown .user-info {
        padding: 4px 8px;
        
        .user-name {
          display: none; // 在很小的屏幕上隐藏用户名，只显示头像
        }
      }
    }
  }
  
  .home-title {
    font-size: 1.5rem;
    letter-spacing: 2px;
  }
}
</style>

